//
//  APNavigationBar.m
//  AqualPolo
//
//  Created by dong on 2024/5/2.
//

#import "APNavigationBar.h"

@interface APNavigationBar ()
@property (nonatomic, strong) UIView *contentView;
@end

@implementation APNavigationBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _contentView = [[UIView alloc] init];
        [self addSubview:_contentView];
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(20);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(44);
            make.bottom.equalTo(self);
        }];
        
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton addTarget:self action:@selector(backButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [_backButton setImage:[UIImage imageNamed:@"back_blue"] forState:UIControlStateNormal];
        [_contentView addSubview:_backButton];
        [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_contentView).offset(20);
            make.top.bottom.equalTo(_contentView);
        }];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [LYFont fontWithSize:18 fontName:LYFontNamepfr];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [_contentView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(_contentView);
            make.left.right.equalTo(self);
        }];
        
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _moreButton.hidden = true;
        [_moreButton setBackgroundImage:[UIImage imageNamed:@"more_white"] forState:UIControlStateNormal];
        [_contentView addSubview:_moreButton];
        [_moreButton addTarget:self action:@selector(moreButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [_moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_contentView).offset(-20);
            make.centerY.equalTo(_contentView);
            make.size.mas_equalTo(CGSizeMake(28, 28));
        }];
        
    }
    return self;
}

- (void)moreButtonDidClick:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(naviViewMoreButtonDidClick)]) {
        [self.delegate naviViewMoreButtonDidClick];
    }
}

- (void)backButtonDidClick:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(backButtonDidClick)]) {
        [self.delegate backButtonDidClick];
    }
}

- (void)safeAreaInsetsDidChange{
    [super safeAreaInsetsDidChange];
    [_contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(self.safeAreaInsets.top);
    }];
}

@end
